import {Observable} from 'rxjs'

// 可以被观察
const observable = new Observable((subscriber)=>{
    let count = 0;
    let intervalId = setInterval(()=>{
        count++;
        subscriber.next(count);
    },1000)

    // 可以返回一个销毁函数，取消订阅时触发
    return ()=>{
        clearInterval(intervalId);
        console.log('unsubscribe')
    }
})

const subscription = observable.subscribe((value:number)=>{
    console.log(value)
    if(value > 5){
        // 取消订阅
        subscription.unsubscribe();
    }
})
